alter table tb_saleorderdtl with check add check(relationqty>=0)
alter table tb_saledtl with check add check(relationqty>=0)
alter table tb_salesendoutdtl with check add check(relationqty>=0)
alter table tb_salequotedtl with check add check(relationqty>=0)
alter table tb_purorderdtl with check add check(relationqty>=0)
alter table tb_purvaluedtl with check add check(relationqty>=0)
alter table tb_purchasedtl with check add check(relationqty>=0)
alter table tb_depotdtl with check add check(relationqty>=0)
alter table tb_otherrecdtl with check add check(relationqty>=0)
alter table tb_othersenddtl with check add check(relationqty>=0)


alter function f_getdtlamt(@btype int,@bid int)
RETURNS decimal(22,2)
as
begin
  declare @billamt decimal(22,2)
  set @billamt=0
  if @btype=240 
    select @billamt=SUM(amount) from tb_saledtl where bid=@bid and isnull(present,0)=0
  RETURN @billamt  
end
alter table tb_sale with check add check(bstate=1 and amount=dbo.f_getdtlamt(240,bid))


alter table tb_sale drop CK__tb_sale__1190E544
